/**
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-01-24 11:26:14
 * @Description  : 存储源实体
 */

const mongoose = require('mongoose')

const storageOriginSchema = new mongoose.Schema(
  {
    type: {
      type: String,
      required: true
    },
    name: {
      type: String,
      required: true
    },
    sizeMax: {
      type: BigInt,
      required: true
    },
    path: {
      type: String,
      required: true
    },
    isDefault: {
      type: Number,
      required: true
    },
    createUser: {
      type: BigInt,
      required: true
    },
    updateUser: {
      type: BigInt,
      required: true
    }
  },
  {
    timestamps: {
      createdAt: 'createTime',
      updatedAt: 'updateTime'
    },
    strict: false
  }
)

// 在查询之后调用的中间件
storageOriginSchema.post('find', function (docs) {
  docs.forEach((doc) => {
    doc.sizeMax = '' + doc.sizeMax
    doc.createUser = '' + doc.createUser
    doc.updateUser = '' + doc.updateUser
  })
})

storageOriginSchema.pre('save', function (next) {
  this.sizeMax = BigInt(this.sizeMax)
  this.createUser = BigInt(this.createUser)
  this.updateUser = BigInt(this.updateUser)
  next()
})

module.exports = storageOriginSchema
